package com.tongji.housediy.service
{   	
	import com.tongji.housediy.controller.brandCMD.GetGoodsByBrandCMD;
	import com.tongji.housediy.events.TransferPicModelEvent;
	import com.tongji.housediy.events.commonEvent.RotationRelatedEvent;
	import com.tongji.housediy.events.commonEvent.ShowHideTitleWindowEvent;
	import com.tongji.housediy.events.goodsEvent.GetBrandNameEvent;
	import com.tongji.housediy.events.goodsEvent.GetBrandPicSrcEvent;
	import com.tongji.housediy.events.goodsEvent.GetCollectionEvent;
	import com.tongji.housediy.events.goodsEvent.GetGoodsByDictEvent;
	import com.tongji.housediy.events.goodsEvent.GetGoodsTypeEvent;
	import com.tongji.housediy.events.goodsEvent.SearchGoodsEvent;
	import com.tongji.housediy.model.HouseModel;
	import com.tongji.housediy.model.SearchResultModel;
	import com.tongji.housediy.model.SelectedObjectModel;
	import com.tongji.housediy.view.components.AbstractBorderChild;
	import com.tongji.housediy.view.components.Furniture;
	import com.tongji.housediy.view.components.RoomShape;
	import com.tongji.housediy.vo.Collection;
	import com.tongji.housediy.vo.PicModel;
	
	import flash.system.Security;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.messaging.ChannelSet;
	import mx.messaging.channels.AMFChannel;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.RemoteObject;
	
	import org.robotlegs.mvcs.Actor;
	
	/**
	 * 
	 * @author yanghanbing
	 * 2011/2/15
	 */ 
	public class GoodsService extends Actor
	{
		[Inject]
		public var selectedModel:SelectedObjectModel;
		
		[Inject]
		public var GoodsByBrandModel:SearchResultModel;
		
		[Inject]
		public var houseModel:HouseModel;
		
		public var goodsRemoteObject:RemoteObject;
		
		public function GoodsService()
		{
			super();
			//var amfChannel:Array = [new AMFChannel(null, "http://10.60.0.118/housediy/Gateway.aspx")];
			//var amfChannel:Array = [new AMFChannel(null, "http://localhost/housediy/Gateway.aspx")];

			var amfChannel:Array = [new AMFChannel(null, "http://design.rryjw.com/Gateway.aspx")];
			//var amfChannel:Array = [new AMFChannel(null, "http://ssegrc.gotoip55.com/Gateway.aspx")];
			var channelSet:ChannelSet = new ChannelSet();
			channelSet.channels = amfChannel;
			Security.loadPolicyFile("http://design.rryjw.com/crossdomain.xml");

			goodsRemoteObject = new RemoteObject("fluorine");
			goodsRemoteObject.source = "HouseDiyService.HouseDiyAction";
			goodsRemoteObject.showBusyCursor = true;
			goodsRemoteObject.channelSet = channelSet;
			
			goodsRemoteObject.getOperation("getGoodsByDict").addEventListener(ResultEvent.RESULT, getGoodsByDictResult);
			goodsRemoteObject.getOperation("getGoodsByDict").addEventListener(FaultEvent.FAULT, faultHandler);
			
			goodsRemoteObject.getOperation("getPicModel").addEventListener(ResultEvent.RESULT, getPicModelResult);
			goodsRemoteObject.getOperation("getPicModel").addEventListener(FaultEvent.FAULT, faultHandler);											
			
			goodsRemoteObject.getOperation("searchGoods").addEventListener(ResultEvent.RESULT, searchResultHandler);
			goodsRemoteObject.getOperation("searchGoods").addEventListener(FaultEvent.FAULT, faultHandler);						
		    
			goodsRemoteObject.getOperation("getGoodsTypeXml").addEventListener(ResultEvent.RESULT, getGoodsTypeResult);
			goodsRemoteObject.getOperation("getGoodsTypeXml").addEventListener(FaultEvent.FAULT, faultHandler);
			
			goodsRemoteObject.getOperation("getCollectionByUser").addEventListener(ResultEvent.RESULT, getCollectionResult);
			goodsRemoteObject.getOperation("getCollectionByUser").addEventListener(FaultEvent.FAULT, faultHandler);
			
			goodsRemoteObject.getOperation("addCollection").addEventListener(ResultEvent.RESULT, addCollectionHandler);
			goodsRemoteObject.getOperation("addCollection").addEventListener(FaultEvent.FAULT, faultHandler);
		
		}
		
		/*-------------------外调接口----------------------*/
		
		public function addACollection(collection:Collection):void
		{
			goodsRemoteObject.addCollection(collection);
		}
		
		public function getGoods(dictName:String):void
		{
			goodsRemoteObject.getGoodsByDict(dictName);
		}
		
		public function getPicModel(sizeID:int, colorID:int):void
		{
			goodsRemoteObject.getPicModel(sizeID, colorID);
		}
		
		public function searchGoodsByName(searchType:Boolean, searchName:String):void
		{
			goodsRemoteObject.searchGoods(searchType, searchName);			
		}
		
		public function getGoodsType():void
		{
			goodsRemoteObject.getGoodsTypeXml();
		}
		
		public function getCollection(userID:String):void
		{
			goodsRemoteObject.getCollectionByUser(userID);
		} 
		/*-------------------外调接口----------------------*/
		
		private function getGoodsByDictResult(event:ResultEvent):void
		{
			dispatch(new GetGoodsByDictEvent(GetGoodsByDictEvent.UPDATE_GOODS_MODEL, null, event.result as ArrayCollection));
		}
		
		private function addCollectionHandler(event:ResultEvent):void
		{
			event.result;
		}
	
		private function getPicModelResult(event:ResultEvent):void
		{
			if (selectedModel.selectedObj is Furniture || selectedModel.selectedObj is AbstractBorderChild)
			{
				(selectedModel.selectedObj as RoomShape).changeSizeAndColor(event.result as PicModel);
				
				// 隐藏 TitleWindow 和 RotationBtn
				dispatch(new ShowHideTitleWindowEvent(ShowHideTitleWindowEvent.HIDE_TITLE_WINDOW));
				dispatch(new RotationRelatedEvent(RotationRelatedEvent.HIDE_ROTATION_BTN));
			}
			else
				dispatch(new TransferPicModelEvent(TransferPicModelEvent.TRANSFER_PIC_MODEL, event.result as PicModel));
		}
		//按品牌名搜索返回商品信息
		private function searchResultHandler(event:ResultEvent):void
		{
			dispatch(new SearchGoodsEvent(SearchGoodsEvent.UPDATE_SEARCH_RESULT, false, null, event.result as ArrayCollection));
		}		
		
		private function getGoodsTypeResult(event:ResultEvent):void
		{
			dispatch(new GetGoodsTypeEvent(GetGoodsTypeEvent.UPDATE_GOODS_TYPE, event.result as ArrayCollection));
		}
		
		private function getCollectionResult(event:ResultEvent):void
		{
			dispatch(new GetCollectionEvent(GetCollectionEvent.UPDATE_COLLECTIONS, "", event.result as ArrayCollection));
		}
		
		private function faultHandler(event:FaultEvent):void
		{
			Alert.show(" fault :" + event.toString());
		}
	}
}